package API.DAY8.thread;

public class SyncDemo1 {
    static boolean x1 = false;
    static boolean y1 = false;
    public static void main(String[] args) throws InterruptedException {

        Bank bank = new Bank();
        int sum = 0;
        while (true) {
            sum++;
            Thread x = new Thread(){
                public void run(){
                    x1=bank.getmoney(20000);
                }
            };
            Thread y = new Thread(){
                public void run(){
                    y1=bank.getmoney(20000);
                }
            };
            x.start();
            y.start();

            Thread.sleep(5);
            if(x1&&y1){
                System.out.println("卡bug取款成功,共"+sum+"次");
                break;
            }else{
//                System.out.println("卡bug失败");
                System.out.println(sum);
                x1=false;
                y1=false;
                bank.setMoney(20000);
            }
        }
    }

    }
class Bank{
    private int account = 20000;
    public synchronized boolean getmoney(int money){
        int account = getMoney();
        if(account>=money){
            account-=money;
            Thread.yield();
            setMoney(account);
            return true;
        }
        return false;
    }

    public int getMoney() {
        return account;
    }

    public void setMoney(int money) {
        this.account = money;
    }
}
